package com.hzlj.position.config.controller;


import cn.hutool.core.date.DateUtil;
import com.fz.common.base.dto.DeleteCommonDTO;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.*;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【设备绑定表】接口
 *
 * @author lifh
 * @date 2023-03-13 16:57:40
 */
@Slf4j
@RestController
@RequestMapping("/position/jz/jbxx/device/bind")
public class JzJbxxDeviceBindController {

    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;

    /**
     * 通过id查询设备绑定表
     *
     * @param id id
     * @return 实例对象
     */
    @GetMapping("/{id}")
    public ResultDTO<JzJbxxDeviceBindQueryResultDTO> getJzJbxxDeviceBindById(@PathVariable("id") String id) {
        log.debug("【查询信息】设备绑定表:id={}", id);
        return new ResultDTO<>(jzJbxxDeviceBindService.getJzJbxxDeviceBindById(id));
    }


    /**
     * 通过jzId查询设备绑定表
     */
    @GetMapping("/byJzId")
    public ResultDTO<JzJbxxDeviceBindQueryResultDTO> getJzJbxxDeviceBindByJzId(String jzId, String channel) {
        log.debug("【查询信息】设备绑定表:jzId={},channel={}", jzId, channel);
        JzJbxxDeviceBindQueryResultDTO bind = jzJbxxDeviceBindService.getJzJbxxDeviceBindByJzId(jzId, channel);
        return new ResultDTO<>(bind);
    }

    /**
     * 同步设备绑定表（删除不在矫的配置）
     */
    @GetMapping("/sync")
    public ResultDTO<Integer> sync() {
        log.debug("【删除设备】同步设备绑定表（删除不在矫的配置）");
        return ResultDTO.ok(
                jzJbxxDeviceBindService.deleteJzJbxxDeviceBind()
        );
    }

    /**
     * 保存设备绑定表
     *
     * @param dto 添加的信息
     * @return 实例对象
     */
    @PutMapping("")
    public ResultDTO<JzJbxxDeviceBindQueryResultDTO> saveJzJbxxDeviceBind(@RequestBody JzJbxxDeviceBindAddDTO dto) {
        log.info("【新增信息】设备绑定表={}", dto);
        return new ResultDTO<>(jzJbxxDeviceBindService.saveJzJbxxDeviceBind(dto));
    }


    /**
     * 更新设备绑定表
     *
     * @param dto 更新的信息
     * @return 是否更新成功
     */
    @PostMapping(value = "")
    public ResultDTO<Boolean> updateJzJbxxDeviceBind(@RequestBody JzJbxxDeviceBindUpdateDTO dto) {
        log.info("【更新信息】设备绑定表={}", dto);
        return new ResultDTO<>(jzJbxxDeviceBindService.updateJzJbxxDeviceBind(dto));
    }

    /**
     * 删除设备绑定表
     *
     * @return 是否删除成功
     */
    @DeleteMapping(value = "")
    public ResultDTO<Boolean> deleteJzJbxxDeviceBind(@RequestBody DeleteCommonDTO<String> dto) {
        log.info("【删除信息】设备绑定表={}", dto);
        return new ResultDTO<>(jzJbxxDeviceBindService.deleteJzJbxxDeviceBind(dto));
    }

    /**
     * 分页查询 设备绑定表
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<JzJbxxDeviceBindQueryResultDTO>> pageJzJbxxDeviceBind(JzJbxxDeviceBindSimpleQueryParamDTO dto, PageParamDTO pageParamDTO) {
        return new ResultDTO<>(jzJbxxDeviceBindService.pageJzJbxxDeviceBind(dto, pageParamDTO));
    }


    /**
     * 导出 设备绑定表(按人分组)
     */
    @GetMapping("/group/export")
    public void groupExport(HttpServletResponse response,
                            JzJbxxDeviceBindQueryParamDTO dto) {
        List<JzJbxxDeviceBindWithJzJbxxDTO> list = jzJbxxDeviceBindService.listJzJbxxDeviceBindGroup(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "定位开通",
                "定位开通",
                EasyExcelUtil.Field.<JzJbxxDeviceBindWithJzJbxxDTO>of()
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getXm, "姓名")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getJgMc, "机构")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getGllbText, "管理类别")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getChannelsText, "定位类型")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getFirstLocateTime, "首次定位时间")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getTurnOn, "是否开通")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getDeviceCodes, "设备编号")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getCrossDistance, "可越界距离")
                        .getFields(),
                list
        );
    }

    /**
     * 导出 人员设备绑定表(按人分组)
     */
    @GetMapping("/group/export2")
    public void groupExport2(HttpServletResponse response,
                             JzJbxxDeviceBindQueryParamDTO dto) {
        List<JzJbxxDeviceBindWithJzJbxxDTO> list = jzJbxxDeviceBindService.listJzJbxxDeviceBindGroup(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "人员列表",
                "人员列表",
                EasyExcelUtil.Field.<JzJbxxDeviceBindWithJzJbxxDTO>of()
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getXm, "姓名")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getXbText, "性别")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getJgMc, "矫正机构")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getChannelsText, "定位类型")
                        .add((e) -> DateUtil.formatDate(e.getJzksrq()), "入矫日期")
                        .add((e) -> DateUtil.formatDate(e.getJzjsrq()), "解矫日期")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getJzztText, "人员状态")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getFirstLocateTime, "首次定位时间")
                        .getFields(),
                list
        );
    }


    /**
     * 导出 设备绑定表(按人分组)
     */
    @GetMapping("/group/export3")
    public void groupExport3(HttpServletResponse response,
                             JzJbxxDeviceBindQueryParamDTO dto) {
        List<JzJbxxDeviceBindWithJzJbxxDTO> list = jzJbxxDeviceBindService.listJzJbxxDeviceBindGroup(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "定位开通",
                "定位开通",
                EasyExcelUtil.Field.<JzJbxxDeviceBindWithJzJbxxDTO>of()
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getXm, "姓名")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getJgMc, "机构")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getXbText, "性别")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getSjh, "手机号")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getGllbText, "管理类别")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getChannelsText, "定位方式")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getDeviceCodes, "定位设备号")
                        .add(e -> Boolean.TRUE.equals(e.getTurnOn()) ? "是" : "否", "是否开通")
                        .add(JzJbxxDeviceBindWithJzJbxxDTO::getCrossDistance, "可越界距离")
                        .add(e -> Boolean.TRUE.equals(e.getMdy()) ? "是" : "否", "免点验")
                        .getFields(),
                list
        );
    }

    /**
     * 列表查询 设备绑定表(按人分组)
     */
    @GetMapping("/group/list")
    public ResultDTO<List<JzJbxxDeviceBindWithJzJbxxDTO>> gropuPageJzJbxxDeviceBind(JzJbxxDeviceBindQueryParamDTO dto) {
        log.info("【查询列表】信息设备绑定表,JzJbxxDeviceBindQueryParamDTO={}", dto);
        return new ResultDTO<>(jzJbxxDeviceBindService.listJzJbxxDeviceBindGroup(dto));
    }

    /**
     * 分页查询 设备绑定表(按人分组)
     */
    @GetMapping("/group/list/page")
    public ResultDTO<QueryPageResultDTO<JzJbxxDeviceBindWithJzJbxxDTO>> gropuPageJzJbxxDeviceBind(JzJbxxDeviceBindQueryParamDTO dto, PageParamDTO pageParamDTO) {
        log.info("【查询分页列表】信息设备绑定表,JzJbxxDeviceBindQueryParamDTO={},pageParamDTO={}", dto, pageParamDTO);
        return new ResultDTO<>(jzJbxxDeviceBindService.pageJzJbxxDeviceBindGroup(dto, pageParamDTO));
    }

}

